/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package librarystudy.data;

import com.db4o.ObjectContainer;
import com.db4o.Db4o;
import java.util.HashSet;
import java.util.List;
/**
 *
 * @author jmhammond
 */
public class ATag {
    // Using the HashSet collection for the list of books
    // In this way, there can be no repeats
    // A side benefit, is we can change the implementation of this
    // member variable and consumers won't notice the difference
    private HashSet<String> titles;
    private String tagName;

    public ATag(String t, HashSet<String> ts) {
        tagName = t;
        titles = ts;
    }

    public ATag(String tag, String title) {
        tagName = tag;
        titles = new HashSet<String>();
        titles.add(title);
    }

    public static ATag getTemplateTag(String t) {
        ATag tmp = new ATag(t, (HashSet)null);
        return tmp;
    }

    /// Titles ///


    public void addTitlesWithTitles(HashSet<String> t) {
        titles.addAll(t);
    }

    public void addTitle(String t) {
        titles.add(t);
    }

    public void addTitlesFromSpaceDelimitedString(String titleString) {
        // Assume that this string contains all previous, and desired, tags
        titles = new HashSet<String>();
        String[] myTitles = titleString.split("\\s"); // split on whitespace

        for (String title : myTitles) {
            titles.add(title);
        }
    }

    public String getTitlesAsSpaceDelimitedString() {
        String t = new String();

        if (titles != null) {
            for (String title : titles) {
                t += title + " ";
            }
        } else {
            return "";
        }

        return t.trim();
    }

    public String getTitlesFormattedWithAuthorsAsNewLineAndSpaceDelimitedString() {
        String t = new String();

        ObjectContainer db = Db4o.openFile("libraryStudy.db");
        List<Book> previouslySaved;
        String author = "";

        try {
            if (titles != null) {
                for (String title : titles) {
                    author = "";
                    
                    Book b = new Book();
                    b.setTitle(title);
                    previouslySaved = db.queryByExample(b);
                    if (previouslySaved.size() > 0) {
                        author = previouslySaved.get(0).getAuthorFormattedName();
                    }

                    t += "\t" + author + " -- " + title + " \n";
                }
            } else {
                return "";
            }

        } finally {
            db.close();
        }
        
        return t.trim();
    }

    public String[] getTitlesAsStringArray() {
        return titles.toArray(new String[0]);
    }

    public HashSet<String> getTitlesAsArrayList() {
        return titles;
    }


    /// Tags ///
    
    public void setTagName(String t) {
        tagName = t;
    }

    public String getTagName() {
        return tagName;
    }

    @Override
    public String toString() {
        return getTagName() + " (" + titles.size() + ") "  + getTitlesAsSpaceDelimitedString();
    }
}
